const router = require('koa-router')();
const fenceService = require("../service/fenceService.js")
const log = require("../com/logConfig.js")
const result = require("../com/result.js")
router.prefix('/fence')
/**
 * 电子围栏控制器
 */

/**
 * 存电子围栏  
 */
router.post('/saveFence', async (ctx, next) => {
    try{
        let result = fenceService.saveFence(ctx, next);
        return await result;
    }catch(e){
        log.error('存电子围栏异常',e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 查询电子围栏
 */
router.get('/getFence', async (ctx, next) => {
    try{
        let result = fenceService.getFence(ctx, next);
        return await result;
    }catch(e){
        log.error('查询电子围栏异常',e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 修改电子围栏
 */
router.post('/updateFence', async (ctx, next) => {
    try{
        let result = fenceService.updateFence(ctx, next);
        return await result;
    }catch(e){
        log.error('修改电子围栏异常',e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 删除电子围栏
 */
router.delete('/delFence', async (ctx, next) => {
    try{
        let result = fenceService.delFence(ctx, next);
        return await result;
    }catch(e){
        log.error('删除电子围栏异常',e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 查询触发电子围栏报警记录
 */
router.get('/getFenceAlarm', async (ctx, next) => {
    try{
        let result = fenceService.getFenceAlarm(ctx, next);
        return await result;
    }catch(e){
        log.error('查询触发电子围栏报警记录异常',e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 测试一个点是否在电子围栏中
 */
router.get('/test', async (ctx, next) => {
    // global.COM_CONFIG.post({
    //     url: global.COM_CONFIG.activitiService.securityArrival,
    //     data: JSON.stringify({
    //         secuId: '189111',
    //         instanceId: '142903',
    //         name: '我是保安11',
    //         peopleType: 'CODE_SECURITY',
    //         taskType: 'assignee'
    //     })
    // });
    try{
        let result = fenceService.test(ctx, next);
        return await result;
    }catch(e){
        log.error('查询电子围栏异常',e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

module.exports = router